import request from '@/utils/request';
// 一级树
export async function getPrimaryTree() {
  const data = await new Promise<{
    code: number;
    data: string[];
    msg: string;
  }>((resolve, reject) => {
    request('/api/report/cloud_all')
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
  return data;
}

// 二级数
export async function getSecondaryTree(params: { name: string }) {
  const data = await new Promise<{
    code: number;
    data: {
      name: string;
      regions: { [key: string]: number };
    };
    msg: string;
  }>((resolve, reject) => {
    request('/api/report/cloud', {
      params,
    })
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
  return data;
}

// 三级树
export async function getTertiaryTree(params: { name: string; cloud: string }) {
  const data = await new Promise<{
    code: number;
    data: {
      name: string;
      nodes: {
        cloud: string;
        local: string;
        online: boolean;
        outer: string;
        proxy: boolean;
        stable: boolean;
        type: string;
      }[];
    };
    msg: string;
  }>((resolve, reject) => {
    request('/api/report/region', {
      params,
    })
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
  return data;
}

// 配置命令
export async function devopsBatch(params: {
  cloud?: string;
  command: string;
  region?: string;
  nodes?: string[];
}) {
  const data = await new Promise<{ [key: string]: any }>((resolve, reject) => {
    const date = new Date();
    request('/api/devops/batch', {
      method: 'POST',
      data: {
        ...params,
        retry: 1,
      },
    })
      .then(response => {
        resolve(response);
      })
      .catch(error => {
        reject(error);
      });
  });
  return data;
}
